package yt.kpool.activity.setting;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.MainActivity;
import yt.kpool.R;
import yt.kpool.bean.VersionBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.OtherUitl;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.LoadingDialog;
import yt.kpool.view.dialog.UpdateDialog;

/**
 * 关于我们
 */
public class AboutUsActivity extends AppCompatActivity {
    @BindView(R.id.tv_version)
    TextView tv_version;
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_us);
        ButterKnife.bind(this);
        initTitle();
    }

    @OnClick({R.id.btn_version})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_version://版本检查
               getVersionInfo();
                break;
        }}
    private void initTitle() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle("关于我们");
        tv_version.setText(getVerName(this)+"版本");
        loadingDialog = new LoadingDialog(this, 0);
    }
    /**
     * 版本跟新
     */
    private void getVersionInfo() {
        NetHelperNew.GetVersionInfo(new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {

                Log.i("-->","版本更新："+data);
                VersionBean baseBean = MyGson.getInstance().fromJson(data, VersionBean.class);
                if (baseBean.getType()==1)
                {
                    getIsUpdate(baseBean.getData().getVersionCode(),baseBean.getData().getUpdateUrl(),baseBean.getData().getUpdateInfo());
                }

            }

            @Override
            public void onError(String msg) {
                super.onError(msg);

            }
        });

    }
    /**
     * 版本更新
     * @param versioncode 版本号
     * @param url 下载地址
     */
    private void getIsUpdate(String versioncode,String url,String versioninfo) {
        if (versioncode.length() > 0)
            if (Integer.valueOf(versioncode) > Integer.valueOf(OtherUitl.getVersionCode(AboutUsActivity.this))) {
                UpdateDialog updateDialog = new UpdateDialog(AboutUsActivity.this, R.style.dialog, versioninfo, url);
                updateDialog.show();
            }
            else {
                ToastUtil.showToast(AboutUsActivity.this, "当前已经是最新版本");
            }




    }

    /**
     * 获取版本名称
     * @param context
     * @return
     */
    public static String getVerName(Context context) {
        String verName = "";
        try {
            verName = context.getPackageManager().getPackageInfo(
                    "yt.kpool", 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {

        }
        return verName;
    }
}
